package com.dayuanit.dy16.ebook.ebookweb.service.impl;

import com.dayuanit.dy16.ebook.ebookweb.dto.CartDTO;
import com.dayuanit.dy16.ebook.ebookweb.entity.Cart;
import com.dayuanit.dy16.ebook.ebookweb.entity.Commodity;
import com.dayuanit.dy16.ebook.ebookweb.mapper.CartMapper;
import com.dayuanit.dy16.ebook.ebookweb.mapper.CommodityMapper;
import com.dayuanit.dy16.ebook.ebookweb.service.ICardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CardServiceImpl extends BaseService implements ICardService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public void addCard(long commoId, int buyNum, long userId) {
        // 商品是否存在
        Cart cart = cartMapper.getByCommoAndUserId(commoId, userId);
        if (null != cart) {
            //更新数量
            cartMapper.updateBuyNum(cart.getId(), cart.getBuyNum() + buyNum);
            return;
        }

        cart = new Cart();
        cart.setBuyNum(buyNum);
        cart.setCommoId(commoId);
        cart.setCreateTime(new Date());
        cart.setStatus(1);
        cart.setUserId(userId);
        cartMapper.insert(cart);
    }


    @Override
    public List<CartDTO> loadCart(long usreId) {
        List<Cart> carts = cartMapper.listByUserId(usreId);
        return formatCartDTO(carts);
    }
}
